﻿//消息类型
using System;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace WhiteBoard
{
    //消息的抽象类
    [Serializable]
    public abstract class WBMessage
    {
        //枚举消息的类型
        public enum WHITEBOARD_MESSAGE_TYPE
        {
            //画基本图形
            DrawStyle,//画图风格
            DrawMouseDown,//开始
            DrawMouseUp,//结束
            DrawMouseMove,//画线
            WBClearScreen,//清除
            enWBBack,//后退
            enWBProceed,//前进
            enWBDeleteButton,
            DrawImage,//是否允许画图
            allDirective, //全息指令
            //控件拖动到其他容器生成控件
            UiDrafting_DragDrop,
            //容器内的控件移动
            UiMove_MouseDown,
            UiMove_MouseMove,
            UiMove_MouseUp,
            //设置画图背景
            DrawingImage,
            //拍照上传
            ImageUpdate,
            CloseSoket//关闭通知
            
        };

        public abstract WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get;
        }
        public WBMessage()
        {
        }
    }
    //画板风格
    [Serializable]
    public class WBMsgDrawStyle : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.DrawStyle;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public Color drawColor;
        public int lineWidth;
    }
    //画板鼠标按下
    [Serializable]
    public class WBMsgDrawMouseDown : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.DrawMouseDown;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public Point startPoint;
    }

    //画板鼠标抬起
    [Serializable]
    public class WBMsgDrawMouseUp : WBMessage
    {
        //Implement the abstract property 
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.DrawMouseUp;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public Point endPoint;
    }
    //画线
    [Serializable]
    public class WBMsgDrawMouseMove : WBMessage
    {
        //Implement the abstract property 
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.DrawMouseMove;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public Point movPoint;
    }
    //清除屏幕
    [Serializable]
    public class WBMsgClearScreen : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE m_enMsgType = WHITEBOARD_MESSAGE_TYPE.WBClearScreen;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return m_enMsgType;
            }
        }
    }

    //后退
    [Serializable]
    public class WBMsgBack : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.enWBBack;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
    }
    //前进
    [Serializable]
    public class WBMsgProceed : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.enWBProceed;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
    }
    //删除控件
    [Serializable]
    public class enWBDeleteButton : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.enWBDeleteButton;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public String name;
    }

    //是否允许画图
    [Serializable]
    public class DrawImage : WBMessage{
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.DrawImage;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public Boolean isdraw=false;
    }
    
    //控件拖动到其他容器生成控件
    [Serializable]
    public class WBMsgUiDrafting_DragDrop : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.UiDrafting_DragDrop;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public String name;
        public String text;
        public Point p;
    }
    //容器内控件移动
    [Serializable]
    public class WBMsgUiMove_MouseDown : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.UiMove_MouseDown;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public Point p;
    }

    [Serializable]
    public class WBMsgUiMove_MouseMove : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.UiMove_MouseMove;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public String name;
        public MouseButtons button;
        public Point mousePos;
    }
    [Serializable]
    public class WBMsgUiMove_MouseUp : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.UiMove_MouseUp;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }

    }

    //设置画图背景
    [Serializable]
    public class WBMsgDrawingImage : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.DrawingImage;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public String type;//选择图片,切图
        public byte[] im;//切图 时图片
        public String path;//选择图片路径
    }

    //拍照上传
    [Serializable]
    public class WBMsgImageUpdate : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.ImageUpdate;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public byte[] im;
        public string name;
    }
    //全息指令
    [Serializable]
    public class WBMsgAllDirective : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.allDirective;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
        public byte[] im;
        public string name;
    }

    
    //服务器关闭通知
    [Serializable]
    public class WBMsgCloseSoket : WBMessage
    {
        private const WHITEBOARD_MESSAGE_TYPE msgType = WHITEBOARD_MESSAGE_TYPE.CloseSoket;
        public override WHITEBOARD_MESSAGE_TYPE MessageType
        {
            get
            {
                return msgType;
            }
        }
    }
    public class WBMessageHelper
    {
        public static MemoryStream Serialize(WBMessage msg)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
            formatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
            switch (msg.MessageType)
            {
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.DrawStyle:
                    formatter.Serialize(ms, (WBMsgDrawStyle)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.DrawMouseDown:
                    formatter.Serialize(ms, (WBMsgDrawMouseDown)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.DrawMouseUp:
                    formatter.Serialize(ms, (WBMsgDrawMouseUp)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.DrawMouseMove:
                    formatter.Serialize(ms, (WBMsgDrawMouseMove)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.enWBBack:
                    formatter.Serialize(ms, (WBMsgBack)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.enWBProceed:
                    formatter.Serialize(ms, (WBMsgProceed)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.UiDrafting_DragDrop:
                    formatter.Serialize(ms, (WBMsgUiDrafting_DragDrop)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.UiMove_MouseDown:
                    formatter.Serialize(ms, (WBMsgUiMove_MouseDown)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.UiMove_MouseMove:
                    formatter.Serialize(ms, (WBMsgUiMove_MouseMove)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.UiMove_MouseUp:
                    formatter.Serialize(ms, (WBMsgUiMove_MouseUp)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.WBClearScreen:
                    formatter.Serialize(ms, (WBMsgClearScreen)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.DrawingImage:
                    formatter.Serialize(ms, (WBMsgDrawingImage)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.ImageUpdate:
                    formatter.Serialize(ms, (WBMsgImageUpdate)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.CloseSoket:
                    formatter.Serialize(ms, (WBMsgCloseSoket)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.DrawImage:
                    formatter.Serialize(ms, (DrawImage)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.allDirective:
                    formatter.Serialize(ms, (WBMsgAllDirective)msg);
                    break;
                case WBMessage.WHITEBOARD_MESSAGE_TYPE.enWBDeleteButton:
                    formatter.Serialize(ms, (enWBDeleteButton)msg);
                    break;
                    
            }
            return ms;
        }

        public static MemoryStream Serialize(long lObject)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
            formatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
            formatter.Serialize(ms, lObject);
            return ms;
        }

        public static WBMessage Deserialize(byte[] bytes, int offset, int count)
        {
            MemoryStream ms = new MemoryStream(bytes, offset, count);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
            formatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
            WBMessage msg;
            msg = (WBMessage)formatter.Deserialize(ms);
            return msg;
        }

        public static long DeserializeL(byte[] bytes, int offset, int count)
        {
            long lOriginal;
            MemoryStream ms = new MemoryStream(bytes, offset, count);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
            formatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
            lOriginal = (long)formatter.Deserialize(ms);
            ms.Close();
            return lOriginal;
        }

    }

}
